package com.ja.openchat.viewmodels;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.ja.openchat.ui.found.CircleDetailActivity;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.model.circle.BlogInfo;
import com.zy.openchat.core.model.photo.PhotoInfo;
import com.zy.openchat.core.model.video.VideoInfo;
import com.zy.openchat.util.Common;

import me.goldze.mvvmhabit.base.MultiItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.KLog;

public class MyAlbumItemViewModel extends MultiItemViewModel<MyAlbumViewModel> {
    public BlogInfo circleInfo;
    public ObservableField<String> mediaUrls = new ObservableField<>();
    public ObservableField<Integer> vedioPlayBtnVisible = new ObservableField<>(View.GONE);


    public MyAlbumItemViewModel(@NonNull MyAlbumViewModel viewModel, BlogInfo entity) {
        super(viewModel);
        this.circleInfo = entity;
        getMediaUrl();
    }

    /**
     * 获取position的方式有很多种,indexOf是其中一种，常见的还有在Adapter中、ItemBinding.of回调里
     *
     * @return
     */
    public int getPosition() {
        return viewModel.getItemPosition(this);
    }

    //条目的点击事件
    public BindingCommand itemClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
//            ToastUtils.showShort("itemClick");
            Bundle bundle = new Bundle();
            bundle.putLong("blogId", circleInfo.id);
            bundle.putInt("type", 0);
            viewModel.startActivity(CircleDetailActivity.class, bundle);
        }
    });
    //条目的长按事件
    public BindingCommand itemLongClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
        }
    });

    public String getMediaUrl() {
        String mediaUrl = null;
        vedioPlayBtnVisible.set(View.GONE);
        if (circleInfo != null && circleInfo.content != null) {
            if (!Common.isEmpty(circleInfo.content.photos)) {
                PhotoInfo photoInfo = circleInfo.content.photos.get(0);
                if (photoInfo.isPhotoDownloaded()) {
                    mediaUrl = photoInfo.getPhotoImagePath();
                } else {
                    TelegramManager.getTelegramManager().downloadFile("blog_" + circleInfo.id, photoInfo.getPhotoFileId()
                            , 0, TelegramManager.FileType.FileType_Message_Photo);
                }
            } else if (circleInfo.content.video != null) {
                vedioPlayBtnVisible.set(View.VISIBLE);
                VideoInfo videoInfo = circleInfo.content.video;
                if (videoInfo.isVideoDownloaded()) {
                    mediaUrl = videoInfo.localVideoPath();
                } else {
                    TelegramManager.getTelegramManager().downloadFile("blog_" + circleInfo.id
                            , videoInfo.getVideoFileId(), videoInfo.downloadedStart()
                            , TelegramManager.FileType.FileType_Message_Video);
                }
            }

        }
        this.mediaUrls.set(mediaUrl);
        KLog.d("update mediaUrl==" + mediaUrl);
        return mediaUrl;
    }
}
